//类HardInfoClass代码如下

using System;
using System.Collections.Generic;
using System.IO;
using System.Management; //需要在解决方案中引用System.Management.DLL文件
using System.Runtime.InteropServices;

namespace BTS.Helpers
{
    /// <summary>
    /// HardInfoClass 的摘要说明。
    /// </summary>
    public class HardInfoClass
    {
        [DllImport("kernel32.dll")]
        private static extern int GetVolumeInformation(
         string lpRootPathName,
         string lpVolumeNameBuffer,
         int nVolumeNameSize,
         ref int lpVolumeSerialNumber,
         int lpMaximumComponentLength,
         int lpFileSystemFlags,
         string lpFileSystemNameBuffer,
         int nFileSystemNameSize
         );

        public HardInfoClass()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 取机器名
        /// </summary>
        /// <returns></returns>
        public string GetHostName()
        {
            return System.Net.Dns.GetHostName();
        }

        //取CPU编号
        public string GetCpuID()
        {
            try
            {
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();

                string strCpuID = null;
                foreach (ManagementObject mo in moc)
                {
                    strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                    break;
                }
                return strCpuID;
            }
            catch
            {
                return "";
            }
        }

        public string GetCpuName()
        {
            try
            {
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();

                string strCpuID = null;
                foreach (ManagementObject mo in moc)
                {
                    strCpuID = mo["Name"].ToString();
                    break;
                }
                return strCpuID;
            }
            catch
            {
                return "";
            }
        }

        //取第一块硬盘编号
        public string GetHardDiskID()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
                string strHardDiskID = null;
                foreach (ManagementObject mo in searcher.Get())
                {
                    strHardDiskID = mo["SerialNumber"].ToString().Trim();
                    break;
                }
                return strHardDiskID;
            }
            catch
            {
                return "";
            }
        }

        //获取网卡MAC地址
        public string GetNetCardMAC()
        {
            try
            {
                string stringMAC = "";
                ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection MOC = MC.GetInstances();

                foreach (ManagementObject MO in MOC)
                {
                    if ((bool)MO["IPEnabled"] == true)
                    {
                        stringMAC += MO["MACAddress"].ToString();
                    }
                }
                return stringMAC;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 获取硬盘信息的代码
        /// </summary>
        /// <param name="drvID"></param>
        /// <returns></returns>
        public string GetVolOf(string drvID)
        {
            try
            {
                const int MAX_FILENAME_LEN = 256;
                int retVal = 0;
                int a = 0;
                int b = 0;
                string str1 = null;
                string str2 = null;

                int i = GetVolumeInformation(
                 drvID + @":/",
                 str1,
                 MAX_FILENAME_LEN,
                 ref retVal,
                 a,
                 b,
                 str2,
                 MAX_FILENAME_LEN
                 );

                return retVal.ToString("x");
            }
            catch
            {
                return "";
            }
        }

        //获取当前网卡IP地址
        public string GetNetCardIP()
        {
            try
            {
                string stringIP = "";
                ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection MOC = MC.GetInstances();

                foreach (ManagementObject MO in MOC)
                {
                    if ((bool)MO["IPEnabled"] == true)
                    {
                        string[] IPAddresses = (string[])MO["IPAddress"];
                        if (IPAddresses.Length > 0)
                            stringIP = IPAddresses[0].ToString();
                    }
                }
                return stringIP;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 判断是否为64操作系统
        /// </summary>
        /// <returns></returns>
        public string GetOSIs64bit()
        {
            string is64Bit = String.Empty;

            try
            {
                is64Bit = Environment.Is64BitOperatingSystem.ToString();
                return is64Bit;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        /// <summary>
        /// 获取内存容量
        /// </summary>
        /// <returns></returns>
        public string GetMemory()
        {
            string stringMemory = "";

            ManagementClass mc = new
            ManagementClass("Win32_ComputerSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            try
            {
                foreach (ManagementObject mo in moc)
                {
                    stringMemory = (((UInt64)mo["TotalPhysicalMemory"]) / Math.Pow(2, 30)).ToString("f2");
                }
                return stringMemory + "GB";
            }
            catch (Exception e)
            {
                return "";
            }
        }

        /// <summary>
        /// 获取操作系统名字
        /// </summary>
        /// <returns></returns>
        public string GetOSName()
        {
            string OSName = "";
            try
            {
                ManagementClass manag = new ManagementClass("Win32_OperatingSystem");
                ManagementObjectCollection managCollection = manag.GetInstances();

                foreach (ManagementObject m in managCollection)
                {
                    OSName = m["Name"].ToString();
                    break;
                }

                if (OSName.Contains("|"))
                    OSName = OSName.Substring(0, OSName.IndexOf('|'));
                return OSName;
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 屏幕分辨率
        /// </summary>
        /// <returns></returns>
        public string DesktopMonitor()
        {
            try
            {
                string DesktopMonitor = "";
                ManagementClass manage = new ManagementClass("Win32_DesktopMonitor");
                ManagementObjectCollection manageCollection = manage.GetInstances();
                foreach (ManagementObject m in manageCollection)
                {
                    DesktopMonitor = m["ScreenWidth"].ToString() + "*" + m["ScreenHeight"].ToString();
                    break;
                }
                return DesktopMonitor;
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 显卡芯片和显存
        /// </summary>
        /// <returns></returns>
        public string VideoAndAdapterRAM()
        {
            try
            {
                string strResult = "";
                ManagementClass manage = new ManagementClass("Win32_VideoController");
                ManagementObjectCollection manageCollection = manage.GetInstances();
                foreach (ManagementObject m in manageCollection)
                {
                    strResult = m["VideoProcessor"].ToString().Replace("Family", "") + "/" + ((Convert.ToInt64(m["AdapterRAM"])) / Math.Pow(2, 30)).ToString("f2") + "GB";
                    break;
                }
                return strResult;
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 获取硬盘分区
        /// </summary>
        /// <returns></returns>
        public string GetDirver()
        {
            try
            {
                string[] s1 = Directory.GetLogicalDrives();
                string strValues = "";
                foreach (string s in s1)
                    strValues += s + ",";
                return strValues.TrimEnd(',');
            }
            catch (Exception)
            {
                return "";
            }
        }

        /// <summary>
        /// 枚举win32 api
        /// </summary>
        private enum HardwareEnum
        {
            // 硬件
            Win32_Processor, // CPU 处理器

            Win32_PhysicalMemory, // 物理内存条
            Win32_Keyboard, // 键盘
            Win32_PointingDevice, // 点输入设备，包括鼠标。
            Win32_FloppyDrive, // 软盘驱动器
            Win32_DiskDrive, // 硬盘驱动器
            Win32_CDROMDrive, // 光盘驱动器
            Win32_BaseBoard, // 主板
            Win32_BIOS, // BIOS 芯片
            Win32_ParallelPort, // 并口
            Win32_SerialPort, // 串口
            Win32_SerialPortConfiguration, // 串口配置
            Win32_SoundDevice, // 多媒体设置，一般指声卡。
            Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)
            Win32_USBController, // USB 控制器
            Win32_NetworkAdapter, // 网络适配器
            Win32_NetworkAdapterConfiguration, // 网络适配器设置
            Win32_Printer, // 打印机
            Win32_PrinterConfiguration, // 打印机设置
            Win32_PrintJob, // 打印机任务
            Win32_TCPIPPrinterPort, // 打印机端口
            Win32_POTSModem, // MODEM
            Win32_POTSModemToSerialPort, // MODEM 端口
            Win32_DesktopMonitor, // 显示器
            Win32_DisplayConfiguration, // 显卡
            Win32_DisplayControllerConfiguration, // 显卡设置
            Win32_VideoController, // 显卡细节。
            Win32_VideoSettings, // 显卡支持的显示模式。

            // 操作系统
            Win32_TimeZone, // 时区

            Win32_SystemDriver, // 驱动程序
            Win32_DiskPartition, // 磁盘分区
            Win32_LogicalDisk, // 逻辑磁盘
            Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。
            Win32_LogicalMemoryConfiguration, // 逻辑内存配置
            Win32_PageFile, // 系统页文件信息
            Win32_PageFileSetting, // 页文件设置
            Win32_BootConfiguration, // 系统启动配置
            Win32_ComputerSystem, // 计算机信息简要
            Win32_OperatingSystem, // 操作系统信息
            Win32_StartupCommand, // 系统自动启动程序
            Win32_Service, // 系统安装的服务
            Win32_Group, // 系统管理组
            Win32_GroupUser, // 系统组帐号
            Win32_UserAccount, // 用户帐号
            Win32_Process, // 系统进程
            Win32_Thread, // 系统线程
            Win32_Share, // 共享
            Win32_NetworkClient, // 已安装的网络客户端
            Win32_NetworkProtocol, // 已安装的网络协议
            Win32_PnPEntity,//all device
        }

        /// <summary>
        /// WMI取硬件信息
        /// </summary>
        /// <param name="hardType"></param>
        /// <param name="propKey"></param>
        /// <returns></returns>
        private string[] MulGetHardwareInfo(HardwareEnum hardType, string propKey)
        {
            List<string> strs = new List<string>();
            try
            {
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType))
                {
                    var hardInfos = searcher.Get();
                    foreach (var hardInfo in hardInfos)
                    {
                        //  if (hardInfo.Properties[propKey].Value != null)
                        if (hardInfo.Properties[propKey].Value != null && hardInfo.Properties[propKey].Value.ToString().Contains("COM"))
                        {
                            strs.Add(hardInfo.Properties[propKey].Value.ToString());
                        }
                    }
                    searcher.Dispose();
                }

                return strs.ToArray();
            }
            catch
            {
                return strs.ToArray();
            }
        }

        /// <summary>
        /// 串口信息
        /// </summary>
        /// <returns></returns>
        public string[] GetSerialPort()
        {
            return MulGetHardwareInfo(HardwareEnum.Win32_PnPEntity, "Name");
        }
    }
}